package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.FootZnx;
import org.footballmanager.pandafootball.system.form.impl.FootZnxSearchForm;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footZnxSearchForm")
@RequestMapping("/hy/footZnx")
public class HyZnxController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footZnx";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.HY, ENTITYNAME);

    @Autowired
    private FootZnxService footZnxService;

    @Autowired
    private FootUserService footUserService;

    /**
     * 初始化
     *
     * @param map               参数
     * @param footZnxSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootZnxSearchForm footZnxSearchForm) {
        map.addAttribute("userList", footUserService.getall());
        FootZnxSearchForm newFootZnxSearchForm = new FootZnxSearchForm();
        if (footZnxSearchForm != null) {
            newFootZnxSearchForm = footZnxSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootZnxSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, HttpServletRequest request,
                         @ModelAttribute FootZnxSearchForm footZnxSearchForm) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        footZnxSearchForm.setResiver(user.getId());
        footZnxService.search(ENTITYNAME, map, footZnxSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footZnxService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute("SearchForm") FootZnxSearchForm form) {
        if (form.getSelectIds() != null) {
            String[] result = form.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                footZnxService.delete(id, request);
            }
        }
        return REDIRECT;
    }

    @RequestMapping(value = "/readall", method = RequestMethod.POST)
    public String readall(HttpServletRequest request) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        footZnxService.readAll(user.getId(), request);
        return REDIRECT;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, HttpServletRequest request) {
        FootZnx znx = footZnxService.get(id);
        znx.setReaded(true);
        footZnxService.updateNotNull(znx, request);
        map.addAttribute(ENTITYNAME, znx);
        return VIEWPAGE;
    }

    @RequestMapping(value = "/znxyd", method = RequestMethod.POST)
    @ResponseBody
    public String znxyd(@RequestParam Long id, HttpServletRequest request) {
        return footZnxService.znxyd(id, request);
    }


}
